#pragma once
#include <vector>
#include "Utility.h"

namespace bs
{
	class Actor;

	class BattleGround
	{
	public:
		enum GridType
		{
			GT_NULL,
			GT_CUR_ACTOR,
			GT_MOVEABLE,
			GT_ATTACKABLE,
			GT_PLACEABLE
		};

	public:
		BattleGround();
		~BattleGround();

		void Init(int _row, int _col, int _cellWidth, int _cellHeight, int _space);

		void Draw(HDC dc);

		Actor* GetActor(Point pos) { return this->GetActor(pos.x, pos.y); }
		Actor* GetActor(int x, int y);

		void SetActor(Actor* actor, Point pos);
		void RemoveActor(Point pos);

		void SetCurActorGrid(Point grid) { this->curActorGrid = grid; }
		Actor* GetCurActor() { return this->cells[curActorGrid.x+curActorGrid.y*col]; }

		void SetCurGrid(Point grid) { this->curGrid = grid; }
		void SetGrids(std::vector<Point> _grids) { this->grids = _grids; }

		void ShowCurGrid(bool show) { this->showCurPos = show; }
		void ShowGrids(GridType gt) { this->typeToShow = gt; }

		void SetSpaceColor(COLORREF color);
		void SetCurGridColor(COLORREF color);
		void SetCurActorColor(COLORREF color);
		void SetMoveableColor(COLORREF color);
		void SetAttackableColor(COLORREF color);
		void SetPlaceableColor(COLORREF color);

		int XIndexToPos(int i) const { return space+i*(cellWidth+space); }
		int YIndexToPos(int i) const { return space+i*(cellHeight+space); }
		int XIndexToCenterPos(int i) const { return XIndexToPos(i)+cellWidth/2; }
		int YIndexToCenterPos(int i) const { return YIndexToPos(i)+cellHeight/2; }
		int XPosToIndex(int i) const { return (i-space/2)/(cellWidth+space); }
		int YPosToIndex(int i) const { return (i-space/2)/(cellHeight+space); }

		int GetWidth() const { return this->XIndexToPos(col); }
		int GetHeight() const { return this->YIndexToPos(row); }
		int GetRow() const { return this->row; }
		int GetCol() const { return this->col; }
		int GetCellWidth() const { return this->cellWidth; }
		int GetCellHeight() const { return this->cellHeight; }

	private:
		void DrawSpace(HDC dc);
		void DrawCurGrid(HDC dc);
		void DrawCurActorGrid(HDC dc);
		void DrawMoveableGrid(HDC dc);
		void DrawAttackableGrid(HDC dc);
		void DrawPlaceabelGrid(HDC dc);

	private:
		int row;
		int col;
		int cellWidth;
		int cellHeight;
		int space;

		bool showCurPos;
		GridType typeToShow;

		Point curGrid;
		Point curActorGrid;
		std::vector<Point> grids;

		std::vector<Actor*> cells;

		HBRUSH spaceBr;
		HBRUSH curGridBr;
		HBRUSH curActorBr;
		HBRUSH moveableBr;
		HBRUSH attackableBr;
		HBRUSH placeableBr;
	};

}
